package com.corgi.grpc.client;

import com.corgi.grpc.lib.grpc.PersonGrpc;
import com.corgi.grpc.lib.grpc.PersonOuterClass;
import io.grpc.Channel;
import net.devh.springboot.autoconfigure.grpc.client.GrpcClient;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @title: GrpcClientService
 * @description:
 * @author: dengmiao
 * @create: 2019-10-08 17:42
 **/
@Service
public class GrpcClientService {

	@GrpcClient("local-grpc-server")
	protected Channel serverChannel;

	String sendMessage(String name) {
		PersonGrpc.PersonBlockingStub stub= PersonGrpc.newBlockingStub(serverChannel);
		PersonOuterClass.HelloReply response = stub.sayHello(
				PersonOuterClass.HelloRequest
						.newBuilder()
						.setName(
								Optional.ofNullable(name)
										.orElse(""))
						.build());
		return response.getMessage();


	}
}
